package com.moli.controller;

import com.moli.domain.ResponseResult;
import com.moli.domain.dto.ChangeRoleStatusDto;
import com.moli.domain.entity.Role;
import com.moli.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author： XC
 * @Date： 2023-06-09 09:35
 * @Description： 角色控制器
 */

@RestController
@RequestMapping("/system/role")
public class RoleController {

	@Autowired
	private RoleService roleService;


	/**
	 * 所有角色列表
	 *
	 * @return
	 */
	@GetMapping("/listAllRole")
	public ResponseResult listAllRole() {
		List<Role> roles = roleService.selectRoleAll();
		return ResponseResult.okResult(roles);
	}

	/**
	 * 根据角色编号获取详细信息
	 */
	@GetMapping(value = "/{roleId}")
	public ResponseResult getInfo(@PathVariable Long roleId) {
		Role role = roleService.getById(roleId);
		return ResponseResult.okResult(role);
	}

	/**
	 * 修改保存角色
	 */
	@PutMapping
	public ResponseResult edit(@RequestBody Role role) {
		roleService.updateRole(role);
		return ResponseResult.okResult();
	}

	/**
	 * 删除角色
	 *
	 * @param id
	 */
	@DeleteMapping("/{id}")
	public ResponseResult remove(@PathVariable(name = "id") Long id) {
		roleService.removeById(id);
		return ResponseResult.okResult();
	}


	/**
	 * 新增角色
	 */
	@PostMapping
	public ResponseResult add(@RequestBody Role role) {
		roleService.insertRole(role);
		return ResponseResult.okResult();

	}

	/**
	 * 角色列表分页
	 *
	 * @param role
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping("/list")
	public ResponseResult list(Role role, Integer pageNum, Integer pageSize) {
		return roleService.selectRolePage(role, pageNum, pageSize);
	}

	/**
	 * 修改角色状态
	 *
	 * @param roleStatusDto
	 * @return
	 */
	@PutMapping("/changeStatus")
	public ResponseResult changeStatus(@RequestBody ChangeRoleStatusDto roleStatusDto) {
		Role role = new Role();
		role.setId(roleStatusDto.getRoleId());
		role.setStatus(roleStatusDto.getStatus());
		return ResponseResult.okResult(roleService.updateById(role));
	}

}
